﻿using MaterialDesignThemes.Wpf;
using Morin.Services;
using Morin.Shared.Common;
using Morin.Shared.Configs;
using Morin.Shared.Models;
using Stylet;
using System.ComponentModel;
using System.Windows.Data;

namespace Morin.Wpf.ViewModels.Settings;

public class VideoSettingsViewModel(IAppService appService, AppSettingsConfig appSettingsConfig) : Screen
{
    private readonly AppSettingsConfig _appSettingsConfig = appSettingsConfig;

    public MediaSourceModel? MediaSourceItem { get; set; }

    public ICollectionView? MediaCollectionView { get; set; }
    public string? SearchContent { get; set; }
    public BindableCollection<MediaSourceModel>? MediaSources { get; set; }
    public string? Title { get; set; }
    public string? JsonUri { get; set; }
    public string? XmlUri { get; set; }
    public string? ParsingUri { get; set; }

    protected override void OnInitialActivate()
    {
        base.OnInitialActivate();

        var mediaSources = appService.GetMediaSources();

        MediaSources = [.. mediaSources];
        MediaCollectionView = CollectionViewSource.GetDefaultView(MediaSources);
        MediaCollectionView.Filter = (o) =>
        {
            if (string.IsNullOrEmpty(SearchContent)) return true;
            if (o is MediaSourceModel s) return !string.IsNullOrEmpty(s.Title) && s.Title.Contains(SearchContent);
            return true;
        };
    }

    public void OnDialogClosing(object sender, DialogClosingEventArgs e)
    {
        if (e.Parameter == null || !e.Parameter.Equals(true)) return;
        if (string.IsNullOrEmpty(Title)) return;

        var eqItem = MediaSources?.FirstOrDefault(x => x.Title != null && x.Title.Contains(Title));
        if (eqItem != null)
        {
            eqItem.Title = Title;
            eqItem.XmlUri = XmlUri;
            eqItem.JsonUri = JsonUri;
            eqItem.ParsingUri = ParsingUri;
            //   更新缓存
            appService.MediaSourcesAddOrUpdate(eqItem);
        }
        else
        {
            var curMaxId=MediaSources?.Max(x => x.Id);
            if (curMaxId != null) {
                //  新建实体
                var item = new MediaSourceModel
                {
                    Id=curMaxId.Value+1,
                    Title = Title,
                    JsonUri = JsonUri,
                    XmlUri = XmlUri,
                    ParsingUri = ParsingUri
                };
                MediaSources?.Add(item);
                //   更新缓存
                appService.MediaSourcesAddOrUpdate(item);
            }
        }



        //  清空需要填写的数据
        JsonUri = null;
        ParsingUri = null;
        Title = null;
        XmlUri = null;
        //  执行一次查询
        MediaCollectionView?.Refresh();
    }


    public void Remove(MediaSourceModel value)
    {
        MediaSources.Remove(value);
        appService.MediaSourcesTryRemove(value.Id);
    }

    public void DataSynchronous(MediaSourceModel sourceModel)
    {
        Task.Run(async () =>
        {
            if (!string.IsNullOrEmpty(sourceModel.JsonUri))
            {
                var rspData = await HttpsProvider.GetAsync(sourceModel.JsonUri);
                if (rspData != null)
                {
                    var jsonModel = JsonProvider.FromContentToObject<RspVideoListJsonDataModel>(rspData);
                    if (jsonModel != null)
                    {
                        sourceModel.Total = jsonModel.Total;
                    }
                    else
                    {
                        // sourceModel.Total = rspData.Total;
                    }
                }
            }
            else
            {
                if (sourceModel.XmlUri != null)
                {
                    var rspData = await HttpsProvider.GetAsync(sourceModel.XmlUri);
                    if (rspData != null)
                    {
                        var xmlModel = XmlProvider.DESerializer<RspVideoListXmlRootModel>(rspData);
                        sourceModel.Total = int.Parse(xmlModel.Videos.Total);
                    }
                }
            }

            //  缓存
            foreach (var item in MediaSources) appService.MediaSourcesAddOrUpdate(item);
        });
    }
}